package com.java.common.cloud;

import com.alibaba.fastjson.JSON;
import com.java.common.cloud.service.*;
import com.java.common.utils.spring.SpringUtils;
import com.java.project.system.config.service.IConfigService;

/**
 * 文件上传Factory
 */
public final class OSSFactory {

    private static IConfigService configService;

    static {
        OSSFactory.configService = (IConfigService) SpringUtils.getBean(IConfigService.class);
    }

    public static CloudStorageService build() {
        String jsonconfig = configService.selectConfigByKey(CloudConstant.CLOUD_STORAGE_CONFIG_KEY);
        // 获取云存储配置信息
        CloudStorageConfig config = JSON.parseObject(jsonconfig, CloudStorageConfig.class);
        if (config.getType() == CloudConstant.CloudService.LOCAL.getValue()){
            return new LocalCloudStorageService(config);
        } else if (config.getType() == CloudConstant.CloudService.QINIU.getValue()) {
            return new QiniuCloudStorageService(config);
        } else if (config.getType() == CloudConstant.CloudService.ALIYUN.getValue()) {
            return new AliyunCloudStorageService(config);
        } else if (config.getType() == CloudConstant.CloudService.QCLOUD.getValue()) {
            return new QcloudCloudStorageService(config);
        }
        return null;
    }
}
